package com.catherine.mvp_base_component.custom_view;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.catherine.mvp_base_component.R;


/**
 * 项目：digital_wallet
 * 文件描述：自定义搜索框
 * 作者：ljj
 * 创建时间：2020/10/26
 */

public class CusSearchView extends LinearLayout implements View.OnClickListener {

    /**
     * 输入框 
     */
    private EditText etInput;

    /**
     * 删除键 
     */
    private ImageView ivDelete;

    /**
     * 上下文对象 
     */
    private Context mContext;

    /**
     * 搜索回调接口 
     */
    private onSearchViewListener mListener;
    private onSearchViewClearListener mListenerClear;

    /**
     * 设置搜索回调接口 
     *
     * @param listener 监听者 
     */
    public void setSearchViewListener(onSearchViewListener listener) {
        mListener = listener;
    }
    public void setSearchViewClearListener(onSearchViewClearListener listener) {
        mListenerClear = listener;
    }

    public CusSearchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        LayoutInflater.from(context).inflate(R.layout.layout_search_view, this);
        initViews();
    }

    private void initViews() {
        etInput = (EditText) findViewById(R.id.item_ed_input);
        ivDelete = (ImageView) findViewById(R.id.item_iv_delete);
        ivDelete.setOnClickListener(this);
        etInput.addTextChangedListener(new EditChangedListener());
        etInput.setOnClickListener(this);

    }

    private class EditChangedListener implements TextWatcher {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

        }
        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            if (!"".equals(charSequence.toString())) {
                // todo 显示打叉
//                ivDelete.setVisibility(VISIBLE);
                //更新autoComplete数据
                if (mListener != null) {
                    mListener.onQueryTextChange(charSequence + "");
                }
            } else {
                ivDelete.setVisibility(GONE);
            }

        }
        @Override
        public void afterTextChanged(Editable editable) {
            if (!"".equals(editable.toString())) {
                // todo 显示打叉
//                ivDelete.setVisibility(VISIBLE);
            } else {
                ivDelete.setVisibility(GONE);
                if (mListenerClear != null) {
                    mListenerClear.onClearTextChange();
                }
            }
        }
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.item_iv_delete) {
            etInput.setText("");
            if (mListener != null) {
                mListener.onQueryTextChange("");
            }
            ivDelete.setVisibility(GONE);
        }
    }
    /**
     * search view回调方法 
     */
    public interface onSearchViewListener {
        boolean onQueryTextChange(String text);
    }
    public interface onSearchViewClearListener {
        void onClearTextChange();
    }
}